package com.huihuiyun.huihuiyun.server.service;

import com.huihuiyun.huihuiyun.server.domain.HhyMemberShoppingCart;
import com.huihuiyun.huihuiyun.server.repository.HhyMemberShoppingCartRepository;
import com.mark.util.CommonUtil;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Vector;

@Component
@Path("/hhyMemberShoppingCarts")
@Slf4j
public class HhyMemberShoppingCartService {
    @Autowired
    private HhyMemberShoppingCartRepository hhyMemberShoppingCartRepository;
    @Context
    private HttpServletResponse httpServletResponse;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberShoppingCart post(HhyMemberShoppingCart hhyMemberShoppingCart) {
        try {
            hhyMemberShoppingCart.id = CommonUtil.createRandomId();
            hhyMemberShoppingCart = hhyMemberShoppingCartRepository.save(hhyMemberShoppingCart);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.CREATED.getStatusCode());
            return hhyMemberShoppingCart;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public void delete(@PathParam("id") String id) {
        try {
            hhyMemberShoppingCartRepository.delete(id);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberShoppingCart put(HhyMemberShoppingCart hhyMemberShoppingCart) {
        try {
            if (CommonUtil.isEmpty(hhyMemberShoppingCart.id)) {
                hhyMemberShoppingCart.id = CommonUtil.createRandomId();
            }
            hhyMemberShoppingCart = hhyMemberShoppingCartRepository.save(hhyMemberShoppingCart);
            return hhyMemberShoppingCart;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberShoppingCart findOne(@PathParam("id") String id) {
        try {
            HhyMemberShoppingCart hhyMemberShoppingCart = hhyMemberShoppingCartRepository.findOne(id);
            if (hhyMemberShoppingCart == null) {
                JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
                return null;
            } else {
                return hhyMemberShoppingCart;
            }
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<HhyMemberShoppingCart> findAll() {
        try {
            List<HhyMemberShoppingCart> hhyMemberShoppingCartList = hhyMemberShoppingCartRepository.findAll();
            return hhyMemberShoppingCartList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAll")
    public Page<HhyMemberShoppingCart> findAll(@QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyMemberShoppingCart> page = hhyMemberShoppingCartRepository.findAll(pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/branch")
    public void post(List<HhyMemberShoppingCart> hhyMemberShoppingCartList) {
        try {
            hhyMemberShoppingCartRepository.save(hhyMemberShoppingCartList);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findIfHhyMemberShoppingCartByMemberIdAndProductId")
    public HhyMemberShoppingCart findIfHhyMemberShoppingCartByMemberIdAndProductId(@QueryParam("memberId") String memberId, @QueryParam("productId") String productId) {
        try {
            HhyMemberShoppingCart hhyMemberShoppingCart = hhyMemberShoppingCartRepository.findHhyMemberShoppingCartByMemberIdAndProductId(memberId, productId);
            if (hhyMemberShoppingCart == null) {
                hhyMemberShoppingCart = new HhyMemberShoppingCart();
                hhyMemberShoppingCart.id = CommonUtil.createRandomId();
                hhyMemberShoppingCart.memberId = memberId;
                hhyMemberShoppingCart.productId = productId;
                hhyMemberShoppingCart = hhyMemberShoppingCartRepository.save(hhyMemberShoppingCart);
            }
            return hhyMemberShoppingCart;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAllByMemberId")
    public List<HhyMemberShoppingCart> findAllByMemberId(@QueryParam("memberId") String memberId) {
        try {
            List<HhyMemberShoppingCart> hhyMemberShoppingCartList = hhyMemberShoppingCartRepository.findAllByMemberId(memberId);
            return hhyMemberShoppingCartList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return new Vector<>();
        }
    }
}
